package com.gitee.qdbp.general.thirdpart.biz.trade.service;

import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.general.common.api.sequence.model.SimpleSequenceRule;
import com.gitee.qdbp.general.common.api.sequence.service.ISimpleSequenceGenerator;
import com.gitee.qdbp.general.thirdpart.api.trade.model.OrderData;
import com.gitee.qdbp.general.thirdpart.api.trade.model.PaidNotify;
import com.gitee.qdbp.general.thirdpart.api.trade.model.PrepayData;
import com.gitee.qdbp.general.thirdpart.api.trade.model.PrepayParams;
import com.gitee.qdbp.general.thirdpart.api.trade.service.IPaymentBaseService;
import com.gitee.qdbp.general.thirdpart.api.trade.service.IPaymentByOrderTypeService;
import com.gitee.qdbp.tools.utils.DateTools;
import com.gitee.qdbp.tools.utils.RandomTools;
import com.gitee.qdbp.tools.utils.VerifyTools;

/**
 * 支付测试
 *
 * @author zhaohuihua
 * @version 180126
 */
public class TestPaymentBusinessService extends Impl implements IPaymentByOrderTypeService {

    @Override
    public String[] getAbleOrderType() {
        return new String[] { "TEST" };
    }

}

class Impl implements IPaymentBaseService {

    @Autowired
    protected ISimpleSequenceGenerator simpleSequenceGenerator;

    /** 生成付款单编号 **/
    @Override
    public String generatePaymentId(PrepayData data) throws ServiceException {
        String oldPaymentId = data.getPaymentId();
        if (VerifyTools.isBlank(oldPaymentId)) {
            return data.getOrderId() + "01";
        }
        String orderId = data.getOrderId();
        SimpleSequenceRule rule = new SimpleSequenceRule(2, orderId, 2);
        return simpleSequenceGenerator.use(rule).generate("PAYMENT", orderId);
    }

    /** 根据orderType和orderId检查订单, 查询支付信息 **/
    @Override
    public PrepayData onPrepayCheckOrder(PrepayParams params) throws ServiceException {
        PrepayData payment = new PrepayData();

        // 根据orderType和orderId检查订单, 查询支付信息
        String orderTitle = "超便宜的商品";
        String orderContent = "支付流程测试";
        Double totalAmount = RandomTools.generateNumber(2, 6) / 100.00; // 随机2~6分钱
        Date createTime = new Date();

        // 查询订单有效期配置项, 计算过期时间
        int orderExpireTime = 60; // 单位:分钟
        Date expireTime = DateTools.addMinute(createTime, orderExpireTime);

        // 构造返回对象
        payment.setPayChannel(params.getPayChannel());
        payment.setOrderType(params.getOrderType());
        payment.setOrderId(params.getOrderId());
        payment.setOrderTitle(orderTitle);
        payment.setOrderContent(orderContent);
        payment.setTotalAmount(totalAmount);
        payment.setCreateTime(createTime);
        payment.setExpireTime(expireTime);
        return payment;
    }

    /** 订单支付回调时查询订单信息 **/
    @Override
    public OrderData onPaidQueryOrder(PaidNotify params) throws ServiceException {
        return null;
    }

}
